package com.ss.android.account.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.collection.WeakHandler;
import com.ss.android.account.b.OnAccountRefreshListener;
import com.ss.android.account.b.k;
import com.ss.android.account.c.QZone;
import com.ss.android.account.c.QZone.b;
import com.ss.android.account.c.c;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.SharePlatform;
import com.ss.android.article.browser.R;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.messagebus.Subscriber;
import com.ss.android.messagebus.SubscriptionRouter;
import com.ss.android.newmedia.activity.FragmentActivity;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class AuthorizeActivity extends FragmentActivity implements Callback, OnAccountRefreshListener, c {
    final class AuthorizeActivity$a {
        String a;
        boolean b;

        AuthorizeActivity$a() {

        }
    }

    public Spipe a;
    public String b;
    public boolean c;
    WeakHandler d;
    boolean e;
    private boolean f;
    private IWXAPI g;
    private int h;
    private b i;

    public AuthorizeActivity() {

        this.c = false;
        this.f = true;
        this.h = -1;
        this.d = new WeakHandler(((Callback)this));
        this.e = false;
        this.i = new b(this);
        new com.ss.android.account.activity.c(this);
    }

    private AuthorizeActivity$a a(int arg7, Intent arg8) {
        AuthorizeActivity$a v0 = null;
        if(arg7 == -1 && arg8 != null) {
            Bundle v1 = arg8.getExtras();
            if(v1 == null) {
                return v0;
            }

            if(!v1.containsKey("callback")) {
                return v0;
            }

            String v2 = v1.getString("callback");
            String v3 = v1.getString("platform");
            Logger.d("snssdk", "callback: " + v2);
            try {
                Uri v4 = Uri.parse(v2);
                String v1_2 = v4.getScheme();
                if(v1_2 == null) {
                    return v0;
                }

                if(!v1_2.startsWith("snssdk")) {
                    return v0;
                }

                if(TextUtils.isEmpty(v4.getQueryParameter("session_key"))) {
                    v1_2 = v4.getQueryParameter("error_description");
                    if(v1_2 == null) {
                        return v0;
                    }

                    UIUtils.showImageToast(((Context)this), R.drawable.close_popup_textpage, v1_2);
                    return v0;
                }

                AuthorizeActivity$a v1_3 = new AuthorizeActivity$a();
                v1_3.a = v3;
                v1_3.b = "1".equals(v4.getQueryParameter("new_platform"));
                v0 = v1_3;
            }
            catch(Exception v1_1) {
                Logger.d("snssdk", "exception when parsing callback url " + v2);
            }
        }

        return v0;
    }

    public final void a() {
        String v0 = TextUtils.isEmpty(null) ? Spipe.a(this.b) : Spipe.b(this.b);
        Intent v1 = new Intent(((Context)this), AuthActivity.class);
        v1.setData(Uri.parse(v0));
        this.startActivityForResult(v1, 32972);
    }

    public final void a(boolean arg8, String arg9) {
        if(arg8) {
            this.h = 1;
            new k(this, this.a.P, this.a.z, "weixin", arg9, this.a.a(true, "weixin")).start();
        }
        else {
            this.h = 0;
            this.onBackPressed();
        }
    }

    public int getLayout() {
        return R.layout.ib;
    }

    public void handleMsg(Message message) {
        if(this.isViewValid()) {
            if(message.what != 13) {
            }
            else {
                this.e = true;
                this.a();
            }
        }
    }

    public void init() {
        super.init();
        this.mTitleView.setText(com.ss.android.article.browser.R.string.ss_authorize_title);
        this.a = Spipe.inst();
        this.b = this.getIntent().getStringExtra("platform");
        if("weixin".equals(this.b)) {
            String v0 = com.ss.android.account.bb.a().e();
            if(android.support.design.a.isTextEmpty(v0)) {
            }else {
                try {
                    this.g = WXAPIFactory.createWXAPI(((Context) this), v0, true);
                    this.g.registerApp(v0);
                } catch (Throwable v0_1) {
                    v0_1.printStackTrace();
                    this.g = null;
                }
            }
        }
     Spipe v0_2 = this.a;
        String v3 = this.b;
        SharePlatform[] v4 = v0_2.mSharePlatforms;
        int v5 = v4.length;
        int v0_3 = 0;
        while(true) {
            if(v0_3 >= v5) {
                v0_3 = 0;
                break;
            }
            else if(v4[v0_3].mName.equals(v3)) {
                v0_3 = 1;
            }
            else {
                ++v0_3;
                continue;
            }

          break;
        }

        if(v0_3 == 0) {
            this.finish();
        }
        else {
            SubscriptionRouter.addSubscriber(this);
            this.a.addOnAccountRefreshListener(((OnAccountRefreshListener)this));
            this.f = true;
        }
    }

    public void onAccountRefresh(boolean arg4, int arg5) {
        int v2 = R.drawable.close_popup_textpage;
        SubscriptionRouter.c(new com.ss.android.account.bus.event.g());
        if(this.a.d(this.b)) {
            Intent v0 = new Intent();
            if(arg4) {
                v0.putExtra("auth_ext_value", arg5);
            }

            this.setResult(-1, v0);
            this.onBackPressed();
        } else {
            if(this.c) {
                this.c = false;
                if(!"huawei".equals(this.b) && !"flyme".equals(this.b)) {
                    this.a();
                    return;
                }

                UIUtils.showImageToast(this.getApplicationContext(), v2, R.string.ss_states_fail_unknown);
                this.onBackPressed();
                return;
            }

            if(!arg4) {
                if(arg5 == R.string.ss_states_fail_session_expire) {
                    NetworkType v0_1 = NetworkUtils.getNetworkType(((Context)this));
                    if(v0_1 != NetworkType.NONE && v0_1 != NetworkType.WIFI) {
                        arg5 = R.string.ss_states_fail_session_expire_try_wifi;
                    }
                }

                UIUtils.showImageToast(((Context)this), v2, arg5);
            }
            this.onBackPressed();
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        this.f = false;
        if(requestCode == 32974) {
            QZone.a(resultCode, intent, this.i);
        }
        else if(requestCode == 32972) {
            AuthorizeActivity$a v0 = this.a(resultCode, intent);
            if(v0 != null) {
                Spipe v1 = this.a;
                int v0_1 = v1.a(v0.b, v0.a);
                System.currentTimeMillis();
                ++v1.z;
                v1.A = v1.z;
                v1.a(((Context)this), v1.A, v0_1);
            }
            else {
                this.onBackPressed();
            }
        }
        else {
            super.onActivityResult(requestCode, resultCode, intent);
        }
    }

    public void onBackPressed() {
        if(!this.isFinishing()) {
            this.finish();
        }
    }

    @Subscriber public void onBindExistEvent(com.ss.android.account.bus.event.b arg2) {
        this.c = false;
    }

    public void onDestroy() {
        super.onDestroy();
        SubscriptionRouter.removeSubscriber(this);
        if(this.a != null) {
            this.a.b(((OnAccountRefreshListener)this));
        }
    }

    public void onResume() {
        super.onResume();
        if(("weixin".equals(this.b)) && !this.f && this.h == -1) {
            this.onBackPressed();
        }
        else if(this.f) {
            this.f = false;
            if("qzone_sns".equals(this.b)) {
                if(QZone.a(((Activity)this), "get_simple_userinfo,get_user_info,add_share,add_t,add_pic_t,add_topic,get_info,get_other_info,get_fanslist,get_idollist,add_idol,get_repost_list", false)) {
                    return;
                }
                else if(QZone.a(((Activity)this), "get_simple_userinfo,get_user_info,add_share,add_t,add_pic_t,add_topic,get_info,get_other_info,get_fanslist,get_idollist,add_idol,get_repost_list", true)) {
                    return;
                }
            }
            else if("weixin".equals(this.b)) {
                if(this.g != null && (this.g.isWXAppInstalled())) {
                    if(!android.support.design.a.a(((Activity)this), this.g, "snsapi_userinfo", "wx_state")) {
                        this.finish();
                    }
                    else {
                    }

                    return;
                }

                UIUtils.showImageToast(((Context)this), R.drawable.close_popup_textpage, R.string.toast_weixin_not_install);
                this.finish();
                return;
            }

            this.a();
        }
    }
}

